<template>
  <div class="app-container">
    <el-form class="form" label-width="102px" label-position="left">
      <el-form-item label="头像">
        <img class="avatar" :src="userInfo.avatar" />
      </el-form-item>
      <el-form-item label="用户名称">
        <span class="text">{{ userInfo.name }}</span>
        <i @click="handleShowDialog('userNameDialog')" title="点击修改用户名称" class="icon el-icon-edit"></i>
      </el-form-item>
      <el-form-item label="密码">
        <span class="text">*********</span>
        <i @click="handleShowDialog('passwordDialog')" title="点击修改密码" class="icon el-icon-edit"></i>
      </el-form-item>
      <!-- <el-form-item label="账号">
        <span class="text">user17</span>
        <i title="点击修改账号" class="icon el-icon-edit"></i>
      </el-form-item>-->
      <el-form-item label="角色">
        <span class="text">{{ userInfo.roleList[0].name }}</span>
      </el-form-item>
    </el-form>

    <el-dialog :close-on-click-modal="false" width="30%" title="修改用户名称" :visible.sync="userNameDialog">
      <el-form label-width="90">
        <el-form-item label="用户名称：">
          <el-input v-model="userName"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="userNameDialog = false">取 消</el-button>
        <el-button type="primary" @click="handleUserNameSubmit">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog :close-on-click-modal="false" width="30%" title="修改密码" :visible.sync="passwordDialog">
      <el-form :model="pwdForm" :rules="rules" label-width="90">
        <el-form-item label="当前登录密码：" prop="oldPassword">
          <el-input type="password" v-model="pwdForm.oldPassword" placeholder="请输入当前登录密码"></el-input>
        </el-form-item>
        <el-form-item label="新密码：" prop="newPassword">
          <el-input type="password" v-model="pwdForm.newPassword" placeholder="请输入新密码"></el-input>
        </el-form-item>
        <el-form-item label="确认新密码：" prop="confirmPassword">
          <el-input type="password" v-model="pwdForm.confirmPassword" placeholder="请确认新密码"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="passwordDialog = false">取 消</el-button>
        <el-button type="primary" @click="handlePasswordSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Personal",
  data() {
    return {
      userInfo: null,
      userNameDialog: false,
      passwordDialog: false,
      userName: null,
      pwdForm: {
        oldPassword: null,
        newPassword: null,
        confirmPassword: null
      },
      rules: {
        oldPassword: [
          {
            required: true,
            message: "请输入当前登录密码"
          }
        ],
        newPassword: [
          {
            required: true,
            message: "请输入新密码"
          }
        ],
        confirmPassword: [
          {
            required: true,
            message: "两次密码输入不一致",
            validator: (rule, value, callback) => {
              if (value === "") {
                callback(new Error("请确认新密码"));
              } else if (value !== this.pwdForm.newPassword) {
                callback(new Error("两次密码输入不一致"));
              } else {
                callback();
              }
            }
          }
        ]
      }
    };
  },
  created() {
    this.userInfo = this.$store.state.user.userInfo;
  },
  methods: {
    handleShowDialog(dialogName) {
      this[dialogName] = true;
      if (dialogName == "userNameDialog") {
        this.userName = this.userInfo.name;
      }
    },
    handleUserNameSubmit() {},
    handlePasswordSubmit() {}
  }
};
</script>

<style lang="scss" scoped>
.app-container {
  .form {
    width: 30%;
    margin: 40px auto;
  }

  .form .avatar {
    width: 58px;
    height: 58px;
    border-radius: 50%;
  }

  .form /deep/ .el-form-item__label {
    font-size: 14px;
    color: rgba(120, 122, 128, 1);
  }

  .form /deep/ .el-form-item__content {
    display: flex;
    align-items: center;
  }

  .form .text {
    flex: 2;
    font-size: 14px;
    color: rgba(72, 73, 77, 1);
  }

  .form .icon {
    flex: 1;
    font-size: 14px;
    text-align: right;
    cursor: pointer;
  }
}
</style>
